import { _decorator, Component, Node, CCInteger, Vec3 } from 'cc'
const { ccclass, property } = _decorator

@ccclass('BgScroll')
export class BgScroll extends Component {
  @property({ type: Node })
  bg01: Node = null
  @property({ type: Node })
  bg02: Node = null

  @property({ type: CCInteger })
  public speed: number = 100 * 2
  // 可视区高度
  private _height: number = 852

  start() {}

  update(deltaTime: number) {
    const bg01Position: Vec3 = this.bg01.position
    this.bg01.setPosition(
      bg01Position.x,
      bg01Position.y - this.speed * deltaTime,
      bg01Position.z,
    )
    const bg02Position: Vec3 = this.bg02.position
    this.bg02.setPosition(
      bg02Position.x,
      bg02Position.y - this.speed * deltaTime,
      bg02Position.z,
    )
    if (this.bg01.position.y <= -this._height) {
      this.bg01.setPosition(
        this.bg01.position.x,
        this.bg02.position.y + this._height,
        this.bg01.position.z,
      )
    }
    if (this.bg02.position.y <= -this._height) {
      this.bg02.setPosition(
        this.bg02.position.x,
        this.bg01.position.y + this._height,
        this.bg02.position.z,
      )
    }
  }
}
